package com.platform.destiny.dto;

import cn.hutool.core.date.DateTime;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.platform.destiny.exception.RetCode;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.Date;

@Getter
@Setter
// 添加 JsonInclude 后，如果字段为空，则不会返回
// @JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> implements Serializable {


    private Integer code;

    private String message;

    private T data;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date timestamp;

    public static <T> Result<T> success(T data){

        Result<T> res = new Result<>();
        res.setCode(200);
        res.setMessage("成功");
        res.setTimestamp(new DateTime());
        res.setData(data);
        return res;
    }

    public static <T> Result<T> failure(String message){

        Result<T> res = new Result<>();
        res.setCode(-1);
        res.setMessage(message);
        res.setTimestamp(new DateTime());
        return res;
    }

    public static <T> Result<T> failure(Integer code, String message){

        Result<T> res = new Result<>();
        res.setCode(code);
        res.setMessage(message);
        res.setTimestamp(new DateTime());
        return res;
    }

    public static <T> Result<T> failure(RetCode code){

        Result<T> res = new Result<>();
        res.setCode(code.code);
        res.setMessage(code.msg);
        res.setTimestamp(new DateTime());
        return res;
    }


    public static <T> Result<T> failure(){
        return failure("请求失败");
    }

}
